#include "ProductTestUartCommon.h"
#include "ProductTestKey.h"
#include "ProductTestUartDataProcess.h"

#define DEF_TAG             "Serial"

Com_u8 g_ProductTest_Uart_FeedResult;
Com_u8 g_ProductTest_Uart_FeedResult_Flag;
Com_s8 g_ProductTest_Uart_Result[PRODUCT_TEST_RESULT_LEN];
Com_s8 g_ProductTest_Uart_Mcu_Ver[MCU_VERSION_LEN];

void Ext_ProductTest_Uart_Set_Feed_Flag(Com_u8 flag)
{
    g_ProductTest_Uart_FeedResult_Flag = flag;
}

Com_u8 Ext_ProductTest_Uart_Get_Feed_Flag(void)
{
    return g_ProductTest_Uart_FeedResult_Flag;
}

void Ext_ProductTest_Uart_Set_FeedResult(Com_u8 res)
{
    g_ProductTest_Uart_FeedResult = res;
}

Com_u8 Ext_ProductTest_Uart_Get_FeedResult(void)
{
    return g_ProductTest_Uart_FeedResult;
}

Com_ps8 Ext_ProductTest_Uart_Result(void)
{
    return g_ProductTest_Uart_Result;
}

Com_ps8 Ext_ProductTest_Uart_Get_Version(void)
{
    return g_ProductTest_Uart_Mcu_Ver;
}

void Ext_ProductTest_Uart_Save_MCU_Version(int ver_num)
{
    char mcu_ver_str[4] = {0};
    sprintf(mcu_ver_str,"%d",ver_num);
    Ext_InComFile_Write(DEF_TXT_MCU_VERSION,mcu_ver_str,sizeof(mcu_ver_str));
}

void Ext_ProductTest_Uart_Save_MCU_Mac(unsigned char * mcu_mac)
{
    char mcu_mac_str[32] = {0};
    sprintf(mcu_mac_str,"%02X:%02X:%02X:%02X:%02X:%02X",mcu_mac[0], mcu_mac[1], mcu_mac[2], mcu_mac[3], mcu_mac[4], mcu_mac[5]);
    printf("get mcu_mac to save: %s\n",mcu_mac_str);

    Ext_InComFile_Write(DEF_TXT_MCU_MAC,mcu_mac_str,sizeof(mcu_mac_str));
}

Com_pvoid ProductTest_Uart_Thread(Com_pvoid arg)
{
    Com_s32 cnt = 0;
    Com_s32 ret;
    Com_s32 serial_fd;
    Com_u8 get_mcu_ver_flag = DEF_COM_FALSE;
    Com_u8 get_uart_result_flag = DEF_COM_FALSE;
    Com_u8 set_video_mode_flag = DEF_COM_FALSE;
    Com_u8 recv_buf[DEF_PER_UART_BUF_SIZE] = {0};

    StComSecondTimer ver_timer;
    StProductTestSpkInfo info;

    memset(&ver_timer,0,sizeof(ver_timer));

    ret = Ext_InPerUartInit_Opt();
    if (ret == -1)
    {
        PRT_ERR(DEF_TAG,"serial init failed!\n");
        return NULL;
    }

    serial_fd = Ext_InPerUartInit_Fd();
    g_ProductTest_Uart_FeedResult = DEF_COM_FALSE;

    strcpy(g_ProductTest_Uart_Result,PRODUCT_TEST_RESULT_ERROR);

    Ext_InComTimer_GetSec(DEF_COM_SET_NEW_TIME,&ver_timer);
    /* 通知MCU开启异常指示灯 */
    Ext_ProductTest_UartHandle_Set_Warning_Led(1);

    DEF_COM_SET_THREAD_NAME("Serial_Com",g_ComThread_Num);

    while(g_ComThread_Running)
    {
        if(cnt <= 100)
        {
            if (cnt == 100)
            {
                /* 5秒后，通知MCU关闭异常指示灯 */
                Ext_ProductTest_UartHandle_Set_Warning_Led(0);
                usleep(1000 * 100);
                Ext_ProductTest_UartHandle_Set_Warning_Led(0);
                usleep(1000 * 100);
                Ext_ProductTest_UartHandle_Set_Warning_Led(0);
            }
            cnt++;
        }

        memset(recv_buf,0,sizeof(recv_buf));
        ret = Ext_InPerUartRead_Process(recv_buf,serial_fd);
        if ((ret > 0) && (ret <= DEF_PER_UART_BUF_SIZE))
        {
            if(get_uart_result_flag == DEF_COM_FALSE)
            {
                strcpy(g_ProductTest_Uart_Result,PRODUCT_TEST_RESULT_OK);
                get_uart_result_flag = DEF_COM_TRUE;
            }

            Com_u8 cmd = recv_buf[3];
            PRT_INFO(DEF_TAG,"cmd: 0x%02x\n",cmd);
            switch(cmd)
            {
            /* mcu return sync time success */
            case COMMAND_SYNC_TIME:
            {
            }
                break;

            case COMMAND_XIAO_AN_CUSTOM_FEED_RESULT:
            case COMMAND_FEED_RESULT:
            {
                Com_u8 status = recv_buf[6];

                /* feed success */
                if(status == 0x00)
                {
                    g_ProductTest_Uart_FeedResult = DEF_COM_TRUE;
                }
                /* feed failed */
                else
                {
                    g_ProductTest_Uart_FeedResult = DEF_COM_FALSE;
                }

                g_ProductTest_Uart_FeedResult_Flag = DEF_COM_TRUE;
            }
                break;

            case COMMAND_CHECK_BLE_VERSION:
            {
                get_mcu_ver_flag = DEF_COM_TRUE;

                Com_s32 ver_num = recv_buf[6];

                memset(g_ProductTest_Uart_Mcu_Ver,0,MCU_VERSION_LEN);
                sprintf(g_ProductTest_Uart_Mcu_Ver,"%d",ver_num);
                printf("mcu version: %s\n",g_ProductTest_Uart_Mcu_Ver);
                Ext_ProductTest_Uart_Save_MCU_Version(ver_num);
            }
                break;

            /** mcu return feed remind **/
            case COMMAND_FEED_REMIND:
            {
            }
                break;

            case COMMAND_MCU_WARNING_LED:
            {
            }
                break;

                /* mcu return network mode */
            case COMMAND_NETWORK_MODE:
            {
            }
                break;

            case COMMAND_CAT_CLOSE_TIME :
            {
                info.path = DEF_WAV_DI_DI;
                info.cnt = 2;
                Ext_ProductTest_Spk_WavPlay_Init(&info);
            }
                break;

            default:
                PRT_WARN(DEF_TAG,"%s no this cmd: 0x%02x\n",__func__,cmd);
                break;
            }
        }
        else if (ret == 0)
        {
            if(set_video_mode_flag == DEF_COM_FALSE)
            {
                static int cnt = 0;

                if(cnt >= 3)
                {
                    set_video_mode_flag = DEF_COM_TRUE;
                }
                Ext_ProductTest_UartHandle_Set_Ble_Mac();
                cnt++;
            }
            else if(get_mcu_ver_flag == DEF_COM_FALSE)
            {
                if(Ext_InComTimer_GetSec(DEF_COM_NOT_SET_TIME,&ver_timer) >= 1)
                {
                    Ext_ProductTest_UartHandle_Check_Version();
                    Ext_InComTimer_GetSec(DEF_COM_SET_NEW_TIME,&ver_timer);
                }
            }
        }
    }

    Ext_InPerUartInit_Close();
    printf("Exit uart test!!\n");
    DEF_COM_EXIT_THREAD(g_ComThread_Num);
}

void Ext_ProductTest_Uart_Init(void)
{
    pthread_t id;

    pthread_create(&id, NULL, ProductTest_Uart_Thread, NULL);
    pthread_detach(id);
}
